<?php

/**
 * 店铺消息
 *
 *
 *
 * * */

namespace app\seller\controller;

class StoreMsg extends \app\common\controller\Seller
{

    public function index()
    {
        $where = array();
        $where['store_id'] = $this->store_id;
        if (!$this->checkIsAdmin() && $this->seller_smt_limits != '*') {
            $where['smt_code'] = array('in', $this->seller_smt_limits);
        }
        $model_storemsg = model('Common/StoreMsg');
        $msg_list = $model_storemsg->getStoreMsgList($where, '*', 10);

        // 整理数据
        if (!empty($msg_list)) {
            foreach ($msg_list as $key => $val) {
                $msg_list[$key]['sm_readids'] = explode(',', $val['sm_readids']);
            }
        }
        $this->assign('msg_list', $msg_list);
        //$this->assign('show_page', $model_storemsg->showpage(2));

        $this->profile_menu('msg_list');
        return $this->fetch();
    }

    /**
     * 消息详细
     */
    public function msg_info()
    {
        $sm_id = input('sm_id/d');
        if ($sm_id <= 0) {
            return $this->error('参数错误');
        }
        $model_storemsg = model('Common/StoreMsg');
        $where = array();
        $where['sm_id'] = $sm_id;
        if ($_SESSION['seller_smt_limits']) {
            $where['smt_code'] = array('in', $_SESSION['seller_smt_limits']);
        }
        $msg_info = $model_storemsg->getStoreMsgInfo($where);
        if (empty($msg_info)) {
            return $this->error('参数错误');
        }
        $this->assign('msg_list', $msg_info);

        // 验证时候已读
        $sm_readids = explode(',', $msg_info['sm_readids']);
        if (!in_array($this->seller_id, $sm_readids)) {
            // 消息阅读表插入数据
            $condition = array();
            $condition['seller_id'] = $this->seller_id;
            $condition['sm_id'] = $sm_id;
            model('Common/StoreMsgRead')->addStoreMsgRead($condition);

            $update = array();
            $sm_readids[] = $this->seller_id;
            $update['sm_readids'] = implode(',', $sm_readids) . ',';
            $model_storemsg->editStoreMsg(array('sm_id' => $sm_id), $update);

            // 清除店铺消息数量缓存
            cookie('storemsgnewnum' . $this->seller_id, null);
        }

        return $this->fetch();
    }

    /**
     * AJAX标记为已读
     */
    public function mark_as_read()
    {
        $smids = input('smids');
        if (!preg_match('/^[\d,]+$/i', $smids)) {
            return showDialog('参数错误', '', 'error');
        }

        $smids = explode(',', $smids);
        $model_storemsgread = Model('Common/StoreMsgRead');
        $model_storemsg = Model('Common/StoreMsg');
        foreach ($smids as $val) {
            $condition = array();
            $condition['seller_id'] = $this->seller_id;
            $condition['sm_id'] = $val;
            $read_info = $model_storemsgread->getStoreMsgReadInfo($condition);
            if (empty($read_info)) {
                // 消息阅读表插入数据
                $model_storemsgread->addStoreMsgRead($condition);

                // 更新店铺消息表
                $storemsg_info = $model_storemsg->getStoreMsgInfo(array('sm_id' => $val));
                $sm_readids = explode(',', $storemsg_info['sm_readids']);
                $sm_readids[] = $this->seller_id;
                $sm_readids = array_unique($sm_readids);
                $update = array();
                $update['sm_readids'] = implode(',', $sm_readids) . ',';
                $model_storemsg->editStoreMsg(array('sm_id' => $val), $update);
            }
        }

        // 清除店铺消息数量缓存
        cookie('storemsgnewnum' . $this->seller_id, null);

        return showDialog('操作成功', 'reload', 'succ');
    }

    /**
     * AJAX删除消息
     */
    public function del_msg()
    {
        // 验证参数
        $smids = input('smids');
        if (!preg_match('/^[\d,]+$/i', $smids)) {
            return showDialog('参数错误', '', 'error');
        }
        $smid_array = explode(',', $smids);

        // 验证是否为管理员
        if (!$this->checkIsAdmin()) {
            return showDialog('权限不足', '', 'error');
        }

        $where = array();
        $where['store_id'] = $this->store_id;
        $where['sm_id'] = array('in', $smid_array);
        // 删除消息记录
        Model('Common/StoreMsg')->delStoreMsg($where);
        // 删除阅读记录
        unset($where['store_id']);
        Model('Common/StoreMsgRead')->delStoreMsgRead($where);
        // 清除店铺消息数量缓存
        cookie('storemsgnewnum' . $this->seller_id, 0, -3600);
        return showDialog('操作成功', 'reload', 'succ');
    }

    /**
     * 消息接收设置
     */
    public function msg_setting()
    {
        // 验证是否为管理员
        if (!$this->checkIsAdmin()) {
            return showDialog('参数错误', '', 'error');
        }

        // 店铺消息模板列表
        $smt_list = Model('Common/StoreMsgTpl')->getStoreMsgTplList(array(), 'smt_code,smt_name,smt_message_switch,smt_message_forced,smt_short_switch,smt_short_forced,smt_mail_switch,smt_mail_forced');

        // 店铺接收设置
        $setting_list = Model('Common/StoreMsgSetting')->getStoreMsgSettingList(['store_id' => $this->store_id], '*', 'smt_code');

        if (!empty($smt_list)) {
            foreach ($smt_list as $key => $val) {
                // 站内信消息模板是否开启
                if ($val['smt_message_switch']) {
                    // 是否强制接收，强制接收必须开启
                    $smt_list[$key]['sms_message_switch'] = $val['smt_message_forced'] ? 1 : intval($setting_list[$val['smt_code']]['sms_message_switch']);

                    // 已开启接收模板
                    if ($smt_list[$key]['sms_message_switch']) {
                        $smt_list[$key]['is_opened'][] = '商家消息';
                    }
                }
                // 短消息模板是否开启
                if ($val['smt_short_switch']) {
                    // 是否强制接收，强制接收必须开启
                    $smt_list[$key]['sms_short_switch'] = $val['smt_short_forced'] ? 1 : intval($setting_list[$val['smt_code']]['sms_short_switch']);

                    // 已开启接收模板
                    if ($smt_list[$key]['sms_short_switch']) {
                        $smt_list[$key]['is_opened'][] = '手机短信';
                    }
                }
                // 邮件模板是否开启
                if ($val['smt_mail_switch']) {
                    // 是否强制接收，强制接收必须开启
                    $smt_list[$key]['sms_mail_switch'] = $val['smt_mail_forced'] ? 1 : intval($setting_list[$val['smt_code']]['sms_mail_switch']);

                    // 已开启接收模板
                    if ($smt_list[$key]['sms_mail_switch']) {
                        $smt_list[$key]['is_opened'][] = '邮件';
                    }
                }

                if (is_array($smt_list[$key]['is_opened'])) {
                    $smt_list[$key]['is_opened'] = implode('&nbsp;|&nbsp;&nbsp;', $smt_list[$key]['is_opened']);
                }
            }
        }
        $this->assign('smt_list', $smt_list);

        $this->profile_menu('msg_setting');
        return $this->fetch();
    }

    /**
     * 编辑店铺消息接收设置
     */
    public function edit_msg_setting()
    {
        // 验证是否为管理员
        if (!$this->checkIsAdmin()) {
            return showDialog('参数错误', '', 'error');
        }
        $code = trim(input('code'));
        if ($code == '') {
            return false;
        }
        // 店铺消息模板
        $smt_info = model('Common/StoreMsgTpl')->getStoreMsgTplInfo(array('smt_code' => $code), 'smt_code,smt_name,smt_message_switch,smt_message_forced,smt_short_switch,smt_short_forced,smt_mail_switch,smt_mail_forced');
        if (empty($smt_info)) {
            return false;
        }

        // 店铺消息接收设置
        $setting_info = model('Common/StoreMsgSetting')->getStoreMsgSettingInfo(array('smt_code' => $code, 'store_id' => $this->store_id));
        $this->assign('smt_info', $smt_info);
        $this->assign('smsetting_info', $setting_info);
        return $this->fetch();
    }

    /**
     * 保存店铺接收设置
     */
    public function save_msg_setting()
    {
        // 验证是否为管理员
        if (!$this->checkIsAdmin()) {
            return showDialog('权限不足', '', 'error');
        }
        $code = trim(input('post.code'));
        if ($code == '') {
            return showDialog('参数错误', 'reload');
        }

        $data = input('post.');
        $obj_validate = new \think\Validate([
            'short_number|手机号码' => ['regex' => '/^13[0-9]{9}$|14[0-9]{9}|15[0-9]{9}$|18[0-9]{9}|17[0-9]{9}$/'],
            'mail_number|邮箱' => 'email'
        ]);
        if (!$obj_validate->check($data)) {
            return showDialog($obj_validate->getError(), 'reload');
        }

        $smt_info = model('Common/StoreMsgTpl')->getStoreMsgTplInfo(array('smt_code' => $code), 'smt_code,smt_name,smt_message_switch,smt_message_forced,smt_short_switch,smt_short_forced,smt_mail_switch,smt_mail_forced');

        // 保存
        $insert = array();
        $insert['smt_code'] = $smt_info['smt_code'];
        $insert['store_id'] = $this->store_id;
        // 验证站内信是否开启
        if ($smt_info['smt_message_switch']) {
            $insert['sms_message_switch'] = $smt_info['smt_message_forced'] ? 1 : intval($data['message_forced']);
        } else {
            $insert['sms_message_switch'] = 0;
        }
        // 验证短消息是否开启
        if ($smt_info['smt_short_switch']) {
            $insert['sms_short_switch'] = $smt_info['smt_short_forced'] ? 1 : intval($data['short_forced']);
        } else {
            $insert['sms_short_switch'] = 0;
        }
        $insert['sms_short_number'] = $data['short_number'] ? $data['short_number'] : '';
        // 验证邮件是否开启
        if ($smt_info['smt_mail_switch']) {
            $insert['sms_mail_switch'] = $smt_info['smt_mail_forced'] ? 1 : intval($data['mail_forced']);
        } else {
            $insert['sms_mail_switch'] = 0;
        }
        $insert['sms_mail_number'] = $data['mail_number'] ? $data['mail_number'] : '';

        model('Common/StoreMsgSetting')->addStoreMsgSetting($insert);
        return showDialog('操作成功', 'reload', 'succ');
    }

    private function checkIsAdmin()
    {
        return $this->store_info['is_admin'] > 0 ? true : false;
    }

    /**
     * 用户中心右边，小导航
     *
     * @param string    $menu_key   当前导航的menu_key
     * @param array     $array      附加菜单
     * @return
     */
    private function profile_menu($menu_key = '')
    {
        $menu_array = array();
        $menu_array = array(
            1 => array('menu_key' => 'msg_list', 'menu_name' => '消息列表', 'menu_url' => url('seller/store_msg/index')),
            2 => array('menu_key' => 'msg_setting', 'menu_name' => '消息接收设置', 'menu_url' => url('seller/store_msg/msg_setting')),
        );

        if (!$this->checkIsAdmin()) {
            unset($menu_array[2]);
        }

        $this->assign('member_menu', $menu_array);
        $this->assign('menu_key', $menu_key);
        $this->assign('left_nav', 'store_msg');
    }

}
